//
//  JHUserViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHUserViewController.h"
#import "JHHotUserMoseTableViewCell.h"
#import "JHHOtMOveTableViewCell.h"
#import "NetwordHandler.h"
#import "UIImageView+WebCache.h"
#import "JHHOTMOve.h"
#import "JHHotUserMove.h"
#import "JHAblumViewController.h"
#import "JHPlayViewController.h"

@interface JHUserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIScrollView *UserScroll;
@property(nonatomic,retain)UIImageView *BactImage;
@property(nonatomic,retain)UIImageView *UserImage;
@property(nonatomic,retain)UITableView *ablumTable;
@property(nonatomic,retain)UITableView *MusicTable;

@property(nonatomic,retain)UIView *view1;
@property(nonatomic,retain)UIView *view2;

/**
 *  个人信息
 */
@property(nonatomic,retain)NSMutableDictionary *dic;
/**
 *  专辑
 */
@property(nonatomic,retain)NSMutableArray *AblumArr;
/**
 *  歌曲
 */
@property(nonatomic,retain)NSMutableArray *gequArr;
@end

@implementation JHUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dbManager = [JHDataBaseManager shareInstance];
    [self.dbManager openDB];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.

//    [self createAblum];
//    [self createMusic];
//     [self createView];
    [self dian];
    [self getData1];
    [self getData2];
    [self getData3];
    [self createScorll];

}
- (void)getData1
{
    NSString *s = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=android&toUid=%@",self.str];
    NSLog(@"%@",s);
    [NetwordHandler requestWithURL:s completion:^(id result) {
//        NSLog(@"11111111111111111111111111%@",result);
//        self.dic = [NSMutableDictionary dictionary];
        self.dic = result;
            [self createView];
    }];
    [self.indicatorView removeFromSuperview];
//    NSLog(@"33333333333333333333333333333333333%@",_dic);
}
- (void)getData2
{
    NSString *s2 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30",self.str];
    [NetwordHandler requestWithURL:s2 completion:^(id result) {
        NSDictionary *d = result;
        NSMutableArray *a = [d objectForKey:@"list"];
        self.gequArr = [NSMutableArray array];
        for (NSDictionary *d2 in a) {
            JHHOTMOve *gequ = [[JHHOTMOve alloc] initWithDic:d2];
            [_gequArr addObject:gequ];
        }
            [self createMusic];
    }];
}
- (void)getData3
{
    NSString *s3 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/1/30",self.str];
    [NetwordHandler requestWithURL:s3 completion:^(id result) {
//        NSLog(@"%@",result);
        NSMutableDictionary *d = result;
        self.AblumArr = [NSMutableArray array];
        NSMutableArray *a = [d objectForKey:@"list"];
        for (NSDictionary *d44 in a) {
            JHHotUserMove *dddd= [[JHHotUserMove alloc] initWithDic:d44];
            [_AblumArr addObject:dddd];
        }[self createAblum];
    }];

}
- (void)createView
{
    
    
    self.BactImage = [[[UIImageView alloc] init] autorelease];
    _BactImage.frame= CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 );
    _BactImage.backgroundColor= [UIColor blackColor];
    NSLog(@"54546565445666%@",[_dic objectForKey:@"backgroundLogo"]);
    if ([[_dic objectForKey:@"backgroundLogo"] isEqualToString:@"http://fdfs.xmcdn.com/group1/M01/00/25/wKgDrVCGfyjgQtMzAAFbcFnzwqk698_large.jpg"]) {
        _BactImage.image = [UIImage imageNamed:@"98a5dd489b92abef8edae6e733b66ce8"];
    }else{
        [_BactImage sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"backgroundLogo"]]];
    }
    [self.view addSubview:_BactImage];
    
    //  小头像部分
    UIImageView *touxiang2 = [[[UIImageView alloc] init] autorelease];
    touxiang2.layer.cornerRadius = 24.5;
    touxiang2.layer.masksToBounds = YES;
    [touxiang2 sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"mobileLargeLogo"]]];
//    NSLog(@"666666666666666%@",[_dic objectForKey:@"mobileLargeLogo"]);
    touxiang2.frame = CGRectMake((self.view.frame.size.width - 60) /2, (_BactImage.frame.size.height - 60) / 3, 60, 60);
    [_BactImage addSubview:touxiang2];
    UILabel *touxianglable = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 150) /2, (_BactImage.frame.size.height - 30) / 4 *3, 150, 30)];
    touxianglable.textAlignment = 1;
//    touxianglable.backgroundColor = [UIColor yellowColor];
    touxianglable.text = [_dic objectForKey:@"nickname"];
    touxianglable.textColor = [UIColor whiteColor];
    [_BactImage addSubview:touxianglable];

}
- (void)createScorll
{
    /**
     滚动视图
     
     :returns: 分段控制器
     */
    UISegmentedControl* Segmented = [[UISegmentedControl alloc] initWithItems:@[@"专辑", @"歌曲"]];
    Segmented.frame = CGRectMake(10, self.view.frame.size.height / 3 - 30 , self.view.frame.size.width - 20, 30);
    Segmented.selectedSegmentIndex = 0;
    Segmented.tintColor = [UIColor lightGrayColor];
    [Segmented addTarget:self action:@selector(segmenAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:Segmented];
    [Segmented release];
    
    self.UserScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, _BactImage.frame.size.height + 200, self.view.frame.size.width, self.view.frame.size.height - _BactImage.frame.size.height - 30)] autorelease];
    _UserScroll.pagingEnabled = YES;
    _UserScroll.bounces = NO;
    _UserScroll.scrollEnabled = NO;
    _UserScroll.showsHorizontalScrollIndicator = NO;
    _UserScroll.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    [self.view addSubview:_UserScroll];
    
    
    /**
     创建
     
     :returns: 两个View  把两个tableView分别加进去
     */
    self.view1 = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _UserScroll.frame.size.height)] autorelease];
//    _view1.backgroundColor = [UIColor yellowColor];
    [_UserScroll addSubview:_view1];
    
    self.view2 = [[[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, _UserScroll.frame.size.height)] autorelease];
    [_UserScroll addSubview:_view2];
    
    
}
- (void)segmenAction:(UISegmentedControl *)seg
{
    //分段控制器点击方法
    if (seg.selectedSegmentIndex == 0) {
        _UserScroll.contentOffset = CGPointMake(0, 0);
    }else
        if (seg.selectedSegmentIndex == 1) {
            _UserScroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        }
}

- (void)createAblum
{
    /**
     创建
     
     :returns: 专辑的tableView
     */
    self.ablumTable = [[UITableView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height - _BactImage.frame.size.height - 30) style:UITableViewStylePlain];
    _ablumTable.delegate = self;
    _ablumTable.dataSource =self;
    
    [_ablumTable registerClass:[JHHotUserMoseTableViewCell class] forCellReuseIdentifier:@"ablumCell"];
    [_view1 addSubview:_ablumTable];
}
- (void)createMusic
{
    /**
     创建
     
     :returns: 歌曲的tableView
     */
    self.MusicTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - _BactImage.frame.size.height - 30) style:UITableViewStylePlain];
    _MusicTable.delegate = self;
    _MusicTable.dataSource =self;
    [_MusicTable registerClass:[JHHOtMOveTableViewCell class] forCellReuseIdentifier:@"musicCell"];
    [_view2 addSubview:_MusicTable];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  重用池
     */
    if (tableView == _ablumTable) {
        JHHotUserMoseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ablumCell"];
        cell.HotUserMove = _AblumArr[indexPath.section];
        /**
         *  收藏
         *
         *  @param buttonAction: 判断
         *
         *  @return 是否已收藏
         */
        [cell.aboutButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        NSArray *arr = [self.dbManager selectInfo];
        [cell.aboutButton setTitle:@"收藏" forState:UIControlStateNormal];
        for (JHAudio *audio in arr) {
            if ([[audio.albumId description] isEqualToString:[cell.HotUserMove.albumId description]]) {
                [cell.aboutButton setTitle:@"已收藏" forState:UIControlStateNormal];
                [cell.aboutButton removeTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                [cell.aboutButton addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        cell.aboutButton.tag = indexPath.section + 1000;
        return cell;
    }else
        if (tableView == _MusicTable) {
            JHHOtMOveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];
            cell.HOTMOve = _gequArr[indexPath.section];
            return cell;
        }return nil;
}

- (void)buttonAction:(UIButton *)button
{
    NSLog(@"点击的第%ld个按钮", button.tag - 1000);
    JHHotUserMove *about = _AblumArr[button.tag - 1000];
    
    [self.dbManager createTable];
    JHAudio *audio = [[JHAudio alloc] init];
    audio.albumId = about.albumId;
    audio.title = about.title;
    [self.dbManager insertInfoWithlanou:audio];
    [button setTitle:@"已收藏" forState:UIControlStateNormal];
    [self.ablumTable reloadData];
}

- (void)buttonAction1:(UIButton *)button
{
    [button setTitle:@"收藏" forState:UIControlStateNormal];
    JHHotUserMove *a = _AblumArr[button.tag - 1000];
    [self.dbManager deleteInfoWithNum:[[a.albumId description] integerValue]];
    [self.ablumTable reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == _ablumTable) {
        return _AblumArr.count;
    }else
        if (tableView == _MusicTable) {
            return _gequArr.count;
        }return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  行数
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _ablumTable) {
        return 80;
    }else
        if (tableView == _MusicTable) {
            return 130;
        }return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  点击方法
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView == _ablumTable) {
        JHAblumViewController *a = [[JHAblumViewController alloc] init];
        a.albumId = [_AblumArr[indexPath.section]albumId];
        [self.navigationController pushViewController:a animated:YES];
    }else
        if (tableView == _MusicTable) {
            JHPlayViewController *aaa = [JHPlayViewController sharePlayerController];
            JHgequ *aaaaa  = [[JHgequ alloc] init];
            
            aaaaa.playPath32 = [_gequArr[indexPath.section]playUrl32];
            aaaaa.title = [_gequArr[indexPath.section]title];
            [aaa setStr:aaaaa];
            [aaa beginAction:self];
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:@"zhuan" object:@"jiaqing" userInfo:@{@"lol":@"666"}];
            [self presentViewController:aaa animated:YES completion:^{
                
            }];
        }

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
